﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProbabilitySequnce
{
    public class Builder2
    {
        public static string[] RandomSequnce(IList<Option> options)
        {
            
            var total = options.Sum(it => it.Count)  ;
            var arr = new string[total];
            foreach (var op in options)
            {
                var step = total/op.Count;

                var cnt = Push(options.IndexOf(op), step, op.Count, op.Key, arr);
                op.Count = op.Count - cnt;
            }
            var ops = options.Where(it => it.Count > 0).ToList();
            var list = Builder.RandomSequnce(ops);
            var index = 0;
            for (var i = 0; i < arr.Length; i++)
            {
                if (arr[i] == null)
                {
                    arr[i] = list[index];
                    index++;
                }
            }
            return arr;
        }

        internal static int Push(int index, int step, int total, string key, string[] arr)
        {
            var count = 0;
            var i = index;
            while (i < arr.Length)
            {
                if (arr[i] == null)
                {
                    arr[i] = key;
                    count+=1;
                    if (count >= total)
                    {
                        break;
                    }
                }
                i += step;
            }
            return count;
        }
    }
}
